package com.zjc.yohoshop.mvp.view;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.MediaController;
import android.widget.RadioGroup;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.zjc.lib_core.base.BaseActivity;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.adapter.HomeMenuAdapter;
import com.zjc.yohoshop.bean.BaseEntity;
import com.zjc.yohoshop.bean.HomeMenuEntity;
import com.zjc.yohoshop.di.compont.DaggerHomeCompont;
import com.zjc.yohoshop.di.modules.HomeModules;
import com.zjc.yohoshop.mvp.contract.HomeContract;
import com.zjc.yohoshop.mvp.persenter.HomePresenter;
import com.zjc.yohoshop.mvp.view.fragment.CategoryFragment;
import com.zjc.yohoshop.mvp.view.fragment.HomeFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import pl.droidsonroids.gif.GifImageView;

/**
 * 首页
 */
public class HomeActivity extends BaseActivity<HomePresenter> implements HomeContract.IHomeView, View.OnClickListener {
    private SlidingMenu slidingMenu;
    private RadioGroup rg_home_bottom;
    private RecyclerView rv_menu;
    private GifImageView giv_home_ufo;
    private FragmentManager manager;
    private HomeFragment homeFragment;
    private CategoryFragment categoryFragment;
    private Fragment current;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int layoutID() {
        return R.layout.activity_home;
    }

    @Override
    public void init() {
        manager = getSupportFragmentManager();
        rg_home_bottom = findViewById(R.id.rg_home_bottom);
        giv_home_ufo = findViewById(R.id.giv_home_ufo);
        homeFragment = new HomeFragment();
        categoryFragment = new CategoryFragment();
        showFrag(homeFragment);
        initmenu();
        EventBus.getDefault().register(this);
    }

    @Override
    public void initData() {
        p.requestMenu();
        initListener();
    }

    @Override
    public void setupDagger() {
        DaggerHomeCompont.builder()
                .homeModules(new HomeModules(this))
                .build().inject(this);
    }

    @Override
    public void success(BaseEntity baseEntity) {
        HomeMenuEntity menuEntity = (HomeMenuEntity) baseEntity;
        //在主线程->刷新UI
        //刷新适配器
        rv_menu.setLayoutManager(new LinearLayoutManager(HomeActivity.this));
        rv_menu.setAdapter(new HomeMenuAdapter(menuEntity.values));
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    /**
     * 事件监听方法
     */
    private void initListener() {
        /**
         * 单选按钮组监听
         */
        rg_home_bottom.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_home_homepage:
                        showFrag(homeFragment);
                        break;
                    case R.id.rb_home_category:
                        showFrag(categoryFragment);
                        break;
                    case R.id.rb_home_see:
                        break;
                    case R.id.rb_home_mine:
                        break;
                }
            }
        });
        /**
         * 默认选中
         */
        rg_home_bottom.check(R.id.rb_home_homepage);

    }

    /**
     * 侧滑
     */
    private void initmenu() {
        //TODO 1:创建菜单对象
        slidingMenu = new SlidingMenu(this);
        //TODO 2：设置属性
        //SlidingMenu.RIGHT   SlidingMenu.LEFT
        slidingMenu.setMode(SlidingMenu.LEFT);//设置从哪边出来
        //TOUCHMODE_FULLSCREEN全屏;TOUCHMODE_MARGIN边界;TOUCHMODE_NONE不能滑动
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);//不能滑动
        slidingMenu.setBehindOffset(200);//单位是像素px 侧滑菜单滑出后，界面剩余的宽度。
        View menu = LayoutInflater.from(HomeActivity.this).inflate(R.layout.layout_sliding_menu, null);
        rv_menu = menu.findViewById(R.id.rv_sliding_menu);
        slidingMenu.setMenu(menu);
        slidingMenu.setShadowWidth(200);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);//绑定到指定的Context
    }

    /**
     * 点击事件监听
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
        }
    }

    /**
     * 显示指定的fragment
     */
    private void showFrag(Fragment fragment){
        FragmentTransaction transaction = manager.beginTransaction();
        if (current != fragment){
            if (current != null && current.isAdded()){
                transaction.hide(current);
            }
            if (!fragment.isAdded()){
                transaction.add(R.id.fl_home,fragment,"");
                transaction.show(fragment);
                transaction.commit();
            }else{
                transaction.show(fragment);
                transaction.commit();
            }
            current = fragment;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openMenu(String str){
        if ("1".equals(str)){
            slidingMenu.showMenu();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
